package com.J.Plane;

import static android.opengl.GLES10.*;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.util.Log;

public class PlaneRenderer implements GLSurfaceView.Renderer {

	private int[] textures = new int[3];
	private Context mContext;
	private SceneNode mNode;
	private SceneNode mNode2;

	/** Brian - Input by touch event*/
	private float _xAngle;
	private float _yAngle;
	
	public void setXAngle(float angle){
		_xAngle = angle;
		
	}
	
	public float getXAngle(){
		return _xAngle;
	}
	
	public void setYAngle(float angle){
		_yAngle = angle;
	}
	
	public float getYAngle(){
		return _yAngle;
	}
	
	
	public PlaneRenderer(Context context) {
		mContext = context;
	}
	
	public PlaneRenderer(){
		
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		gl.glDisable(GL_DITHER);
		gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);

		gl.glClearColor(.5f, .5f, .5f, 1);
		gl.glShadeModel(GL_SMOOTH);
		gl.glEnable(GL_DEPTH_TEST);
		gl.glEnable(GL_TEXTURE_2D);

		BitmapMaterial mat = new BitmapMaterial(gl, mContext.getResources(),
				R.drawable.sohn);
		Plane plane1 = new Plane(4, 4, 4, 4, mat);
		getXAngle();
		mNode = new SceneNode(plane1)
		.translate(1, 0, 0)
		.rotate(_xAngle, 0, 1, 0);
		
		;
		
		mNode2 = new SceneNode(plane1);		
		mNode2.scale(0.3f).translate(0,2.5f,0).rotate(_xAngle, 0, 1, 0);
		
		mNode.addChild(mNode2);
		//mNode.removeChild(mNode2);
	}

	public void onSurfaceChanged(GL10 gl, int w, int h) {
		gl.glViewport(0, 0, w, h);
		float ratio = (float) w / h;
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glFrustumf(-ratio, ratio, -1, 1, 3, 20);
	}

	public void onDrawFrame(GL10 gl) {
		gl.glDisable(GL_DITHER);
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
		gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		gl.glMatrixMode(GL_MODELVIEW);
		gl.glLoadIdentity();
		GLU.gluLookAt(gl, 0, 0, 13, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

		///////////////////////////////////
		mNode.rotate(_xAngle, 0, 1, 0);
		//mNode2.scale(0.3f).translate(0,2.5f,0).rotate(_xAngle, 1, 0, 0);
		
		//mNode2.rotate(_xAngle, 1, 0, 0);
		//////////////////////////////
		
		mNode.render(gl);
		mNode2.render(gl);
	}
	

	
	
	
	
	
	

}
